package com.fundamental.iterator;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 简单四元集合，遍历时每次回随机返回四元集合中的一个元素，直到返回destroy为止
 * @param <T>
 */
public class SimpleCollection<T> implements Iterable<T> {

    T first;
    T second;
    T third;
    final T destroy;

    public SimpleCollection(T first,T second,T third,T destroy){
        this.first = first;
        this.second = second;
        this.third = third;
        this.destroy = destroy;
    }


    class SimpleIterator implements Iterator<T>{
        Random random = new Random();
        T current;
        @Override
        public boolean hasNext() {
            if(current == destroy){
                return false;
            }
            return true;
        }

        @Override
        public T next() {
            current = randomNext();
            return current;
        }

        private T randomNext() {
            if(random.nextBoolean()){
                return first;
            }
            if(random.nextBoolean()){
                return second;
            }
            if(random.nextBoolean()){
                return third;
            }
            return destroy;
        }
    }

    @Override
    public Iterator<T> iterator() {
        return new SimpleIterator();
    }
}
